<template>
	<view>
		<view class="within">
			<view class="search_top">
				<span @click="show_select_city = true" class="search_left">{{city_name}} <u-icon class="icon" size="12"
						name="arrow-down-fill"></u-icon></span>
				<u-search class="search_form" placeholder="大家都在搜索" placeholderColor="#ccc" v-model="keyword"
					:animation="false" :showAction="false" @search="searchHouse()" @clear="clearHouse()"></u-search>
			</view>
		</view>
		<!-- <view class="select_list">
			<view class="text" @click="gender_show = true">{{gender_text}}<u-icon class="icon_1" name="arrow-down-fill"
					color="#333" size="10"></u-icon></view>
			<view class="text" @click="gender_show1 = true">{{gender_text1}}<u-icon class="icon_1"
					name="arrow-down-fill" color="#333" size="10"></u-icon></view>
			<view class="text" @click="gender_show = true">{{gender_text}}<u-icon class="icon_1" name="arrow-down-fill"
					color="#333" size="10"></u-icon></view>
			<view class="text" @click="gender_show = true">{{gender_text}}<u-icon class="icon_1" name="arrow-down-fill"
					color="#333" size="10"></u-icon></view> 
		</view>-->
		<view class="home_list">
			<view class="home_list_within" v-for="item in jsonData" @click="home_list_link(item.id)">
				<view class="home_img">
					<image class="home_img_image" :src="item.project" alt="" mode="aspectFill"></image>
					<span>热销中</span>
				</view>
				<view class="home_text">
					<h2>{{item.title}}</h2>
					<p v-if="item.price"><span>均价:&nbsp;&nbsp;</span>
						<b>{{item.price}}</b>
					</p>
					<p><span>地址:&nbsp;&nbsp;</span>{{item.address}}</p>
					<view class="home_tags">
						<view class="tags_li" v-for="(items,idxs) in item.tags" :key="idxs" v-if="idxs < 2">{{items}}
						</view>
					</view>
				</view>
				<!-- 				<view class="icon_top">
					<u-icon class="icon" name="heart" color="#f48359" size="20"></u-icon>
				</view> -->
			</view>
			<view class="empty" v-if="jsonData.length == 0">
				<u-empty mode="data" text="暂无数据"></u-empty>
			</view>
		</view>
		<u-popup :show="show_select_city" @close="close_select_city" @open="open_select_city">
			<view class="search_box">
				<u-search @change="changeSearch" class="search_city" placeholder="搜索城市" actionText="搜索"
					v-model="keyword_1"></u-search>
				<view class="select_name">
					<view class="local_map">
						<view class="city" @click="select_citys()">当前城市：{{city_name}}</view>
						<view class="city_map" @click="getLocation()">定位</view>
					</view>
					<span>热门城市</span>
					<view @click="select_things(item)" v-for="(item,index) in city_box" :key="index">{{item.title}}
					</view>
				</view>
			</view>
		</u-popup>
		<u-picker :show="gender_show" :columns="gender_arry" @confirm="gender_true"
			@cancel="gender_show = false"></u-picker>
		<u-picker :show="gender_show1" :columns="gender_arry1" @confirm="gender_true1"
			@cancel="gender_show1 = false"></u-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				keyword: "",
				keyword_1: "",
				show_select_city: false,
				city_name: "郑州市",
				city_id: 0,
				city_box: [],
				gender_show: false,
				gender_show1: false,
				gender_text: '均价',
				gender_text1: '户型',
				gender_arry: [
					['2k以下', '2k-3k', '3k-5k', '5k-8k', '8k-10k', '10k-12k', '12k以上']
				],
				gender_arry1: [
					['单间', '一室一厅', '二室一厅', '三室一厅']
				],
				jsonData: [],
				allpage: 0,
				page: 1,
				limit: 5,
			}
		},
		onLoad() {
			let cityId = uni.getStorageSync('city_id');
			let cityName = uni.getStorageSync('city_name');
			let keyword = uni.getStorageSync('keys');
			if (cityId) {
				this.city_id = cityId;
			}
			if (cityName) {
				this.city_name = cityName;
			}
			if (keyword) {
				this.keyword = keyword;
			}
			console.log(this.keyword);
			this.getArea();
			this.getIndex();
		},
		onReachBottom() {
			var _this = this;
			console.log(this.allpage);
			if (this.allpage > this.jsonData.length) {
				_this.getIndex();
			} else {
				_this.ending = true;
			}
		},
		methods: {

			changeSearch(e) {
				console.log(e);
				console.log(this.keyword_1);
				this.getArea();
			},

			searchHouse() {
				console.log(this.keyword);
				this.page = 1;
				this.getIndex();
			},

			clearHouse() {
				uni.removeStorageSync('keys');
				this.keyword = "";
				this.page = 1;
				console.log(this.keyword);
				this.getIndex();
			},

			getArea() {
				uni.$u.http.post("/getArea", {
					keys: this.keyword_1
				}).then((res) => {
					if (res.code == 200) {
						this.city_box = res.data;
					}
				})
			},

			//获取数据
			getIndex() {
				console.log(this.keyword);
				uni.showLoading({
					title: "获取中…"
				})
				uni.$u.http.post("/house/index", {
					keys: this.keyword,
					city_id: this.city_id,
					limit: this.limit,
					page: this.page
				}).then((res) => {
					if (res.code == 200) {
						this.allpage = res.data.total;
						if (this.page == 1) {
							this.jsonData = res.data.data;
							this.page++;
						} else {
							this.jsonData = this.jsonData.concat(res.data.data);
							if (res.data.data.length >= 1) {
								this.page++;
							}
						}
					}
					uni.removeStorageSync('keys');
					uni.hideLoading();
				})
			},


			home_list_link: function(id) {
				uni.navigateTo({
					url: "/pages/good/good_det?id=" + id
				})
			},
			open_select_city() {
				this.keyword_1 = "";
				this.getArea();
				// console.log('open');
			},
			close_select_city() {
				this.show_select_city = false
				// console.log('close');
			},
			select_citys() {
				this.show_select_city = false;
				this.getIndex();
			},
			select_things(item) {
				console.log(item);
				this.show_select_city = false;
				this.city_name = item.title;
				this.city_id = item.id;
				this.page = 1;
				this.jsonData = [];
				uni.setStorageSync('city_id', item.id);
				uni.setStorageSync('city_name', item.title);
				this.getIndex();
			},
			gender_true(e) {
				console.log(e);
				this.gender_text = e.value[0];
				this.gender_show = false;
			},
			gender_true1(e) {
				console.log(e);
				this.gender_text1 = e.value[0];
				this.gender_show1 = false;
			},
			change(e) {
				console.log('change', e);
			},
		}
	}
</script>

<style lang="scss">
	.empty {
		padding: 30rpx 0;
	}

	.search_top {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		line-height: 80rpx;

		.search_left {
			width: 18%;
			margin-right: 20rpx;
			font-size: 24rpx;
			display: flex;
			align-items: center;
		}

		.search_form {}

		.icon {
			display: inline-block;
		}
	}

	.home_list {
		margin: 0 20rpx;

		view.home_list_within {
			border-bottom: 2rpx solid #d0e0e3;
			box-sizing: border-box;
			margin: 30rpx 0;
			width: 100%;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			padding: 0 0 10rpx;

			.home_img {
				width: 220rpx;
				position: relative;

				.home_img_image {
					width: 238rpx;
					height: 307rpx;
					border-radius: 12rpx;
				}

				span {
					position: absolute;
					left: 0;
					top: 0;
					background: #fe8f33;
					text-align: center;
					color: #fff;
					display: block;
					font-size: 20rpx;
					line-height: 36rpx;
					width: 120rpx;
					border-bottom-right-radius: 30rpx;
				}
			}

			.home_text {
				width: calc(100vw - 300rpx);

				.home_tags {
					width: 100%;
					display: flex;
					flex-wrap: wrap;
					padding: 10rpx 0;

					.tags_li {
						background-color: #fff1dc;
						font-size: 12px;
						color: #8c6239;
						margin-right: 16rpx;
						padding: 4rpx 12rpx;
						border-radius: 6rpx;
						margin-bottom: 16rpx;
					}
				}

				h2 {
					font-size: 32rpx;
					border-bottom: 2rpx solid #d0e0e3;
					font-weight: bold;
					padding-bottom: 10rpx;
					line-height: 45rpx;
				}

				p {
					margin-top: 10rpx;
					line-height: 46rpx;
					font-size: 20rpx;
					display: flex;
					flex-direction: row;

					b {
						color: #f05b21;
						font-size: 32rpx;
						font-weight: bold;
					}
				}

				p:nth-child(3) {
					line-height: 33rpx;
					height: 66rpx;
					display: -webkit-box;
					overflow: hidden;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
				}

				p:nth-child(2) span {
					font-size: 28rpx;
				}
			}

			.icon_top {
				padding-top: 20rpx;
			}

			.icon {
				height: 40rpx;
			}

			:last-child {
				border-bottom: none;
			}
		}
	}

	.within {
		padding: 20rpx;
	}

	.search_box {
		width: 100vw;
		height: 100vh;
		box-sizing: border-box;
		padding: 60rpx 0;

		.search_city {
			padding: 0 60rpx;
		}

		.select_name {
			font-size: 24rpx;

			.local_map {
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				margin: 0;
				padding-bottom: 0;
				padding: 10px 40rpx 10px 100rpx;

				.city {
					padding: 0;
					line-height: 52rpx;
				}

				.city_map {
					padding: 0;
					border: 1rpx solid #b1b1b1;
					height: 52rpx;
					line-height: 52rpx;
					padding: 0 16rpx;
					border-radius: 5px;
				}
			}

			view {
				padding: 20rpx 100rpx;
				margin: 20rpx 0;
			}

			span {
				background: #f4f4f4;
				display: block;
				margin: 20rpx 0;
				padding: 20rpx 100rpx;
				color: #ccc;
			}
		}
	}

	.select_list {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		font-size: 24rpx;
		box-shadow: 2rpx 8rpx 12rpx rgba(0, 0, 0, .1);
		line-height: 80rpx;

		.icon_1 {
			display: inline-block;
			margin-top: 8rpx;
			margin-left: 12rpx;
		}

		.text {
			width: 48%;
			text-align: center;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: center;
		}
	}
</style>